﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace TestPaper
{
	public partial class Form1 : Form
	{
		protected static string connectionString = @"provider=microsoft.jet.oledb.4.0; " + 
			"Data Source= " + System.IO.Directory.GetCurrentDirectory() + @"\\paper.mdb";
		private const int ChoicePercent = 49;
		private List<ChoiceClass> choice;
		private List<DecideClass> decide;
			
		public Form1()
		{
			InitializeComponent();
			LoadData(0,flowLayoutPanel1);
		}
		
		private void LoadData(int catalog,FlowLayoutPanel flowPanel)
		{
			//创建数据库连接 
			OleDbConnection aConnection = new OleDbConnection(connectionString);
			
			choice= new List<ChoiceClass>();
			decide= new List<DecideClass>();

			OleDbCommand aCommand_1 = new OleDbCommand("select * from Choice where [Catalog]="+catalog+";", aConnection);
			OleDbCommand aCommand_2 = new OleDbCommand("select * from Decide where [Catalog]="+catalog+";", aConnection); 
			try 
			{
				aConnection.Open(); 
				OleDbDataReader aReader = aCommand_1.ExecuteReader();

				Label item1 = new Label();
				item1.Text="选择题";
				item1.AutoSize=true;
				item1.Margin=new Padding(0,10,0,0);
				bool ChoiceLabel = false;
				while(aReader.Read()) 
				{
					if(ChoiceLabel==false)
					{
						flowPanel.Controls.Add(item1);
						ChoiceLabel=true;
					}
					choice.Add(new ChoiceClass("("+(choice.Count+1)+") "+aReader.GetString(1), aReader.GetInt32(7)));
					for (int i = 3; i < 3+aReader.GetInt32(2); i++)
						choice.Last().AddQuestion(aReader.GetString(i));
					flowPanel.Controls.Add(choice.Last());
				}
				
				aReader = aCommand_2.ExecuteReader();

				Label item2 = new Label();
				item2.Text = "判断题";
				item2.AutoSize = true;
				item2.Margin = new Padding(0, 10, 0, 0);
				bool DecideLabel = false;		
				while(aReader.Read())
				{
					if (DecideLabel == false)
					{
						flowPanel.Controls.Add(item2);
						DecideLabel = true;
					}
					decide.Add(new DecideClass("(" + (decide.Count + 1) + ") " + aReader.GetString(1), aReader.GetBoolean(2)));
					flowPanel.Controls.Add(decide.Last());
				}
				
				aReader.Close(); 
				aConnection.Close(); 
			}
			catch(OleDbException e) 
			{ 
				MessageBox.Show(e.Message);
			} 
  		}
  		
  		private string GetScore(List<ChoiceClass> choice,List<DecideClass> decide,int ChoicePercent)
  		{
  			int choiceRightNum=0;
			int decideRightNum=0;
			foreach (var item in choice)
			{
				choiceRightNum += item.GetScore();
			}

			foreach (var item in decide)
			{
				decideRightNum += item.GetScore();
			}
			double ret=0;
			if (choice.Count > 0 && decide.Count>0) 
			{
				ret+=1.0*ChoicePercent*choiceRightNum/choice.Count;
				ret+=1.0*(100-ChoicePercent)*decideRightNum/decide.Count;
			}
			else if(choice.Count > 0 )
			{
				ret += 1.0 * 100 * choiceRightNum / choice.Count;
			}
			else if(decide.Count > 0)
			{
				ret += 1.0 * 100 * decideRightNum / decide.Count;
			}
			else return "[错误：题库里面还没有任何题目]";
			
			return String.Format("{0:0}", ret);
  		}

		private void button1_Click(object sender, EventArgs e)
		{
			foreach (var item in choice)
			{
				if(item.GetScore()<0)
				{
					MessageBox.Show("对不起，选择题第" + (choice.IndexOf(item) + 1) + "题还没有选择答案", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}
			}

			MessageBox.Show("测试结果：您获得了" + GetScore(choice,decide,ChoicePercent) + "分","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
		}
	}
}
